package naga.x.resource.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;

public class CommonUtils {

	public static final Random RND = new SecureRandom();
	private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	static final char[] DIGITS = {
	        '0' , '1' , '2' , '3' , '4' , '5' ,
	        '6' , '7' , '8' , '9' , 'a' , 'b' ,
	        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
	        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
	        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
	        'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
	        'A' , 'B' , 'C' , 'D' , 'E' , 'F' ,
	        'G' , 'H' , 'I' , 'J' , 'K' , 'L' ,
	        'M' , 'N' , 'O' , 'P' , 'Q' , 'R' ,
	        'S' , 'T' , 'U' , 'V' , 'W' , 'X' ,
	        'Y' , 'Z'
	};
	private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static byte[] encrypt(String algorithm, byte[] input) {
		try {
			MessageDigest md = MessageDigest.getInstance(algorithm);
			return md.digest(input);
		} catch (NoSuchAlgorithmException e) {
		}
		return new byte[0];
	}
	
	public static String encryptToHexString(String algorithm, byte[] input) {
		byte[] data = encrypt(algorithm, input);
		return bytesToHexString(data);
	}
	
	public static String encryptToHexString(String algorithm, String input) {
		byte[] _input = IOUtils.getStringBytes(input, IOUtils.UTF8);
		if (_input == null) {
			return "";
		}
		return encryptToHexString(algorithm, _input);
	}
	
	public static String md5(String input) {
		return encryptToHexString("md5", input);
	}
	
	public static String bytesToHexString(byte[] nums) {
		int len = nums.length;
		char[] buf = new char[len << 1];
		for (int i = 0; i < len; i++) {
			byte n = nums[i];
			buf[i << 1] = HEX_DIGITS[n >>> 4 & 0xf];
			buf[(i << 1) + 1] = HEX_DIGITS[n & 0xf];
		}
		return new String(buf);
	}
	
	public static boolean strEmpty(String str) {
		return str == null || str.isEmpty();
	}
	
	public static boolean strBlank(String str) {
		return str == null || str.trim().isEmpty();
	}
	
	public static Object[] mergeArray(Object[] arr1, Object[] arr2) {
		int len1;
		if (arr1 == null || (len1 = arr1.length) == 0) {
			return arr2;
		}
		int len2;
		if (arr2 == null || (len2 = arr2.length) == 0) {
			return arr1;
		}
		Object[] dest = Arrays.copyOf(arr1, len1 + len2);
		System.arraycopy(arr2, 0, dest, len1, len2);
		return dest;
	}
	
	public static String formatDate(Date date) {
		return date != null ? SDF.format(date) : null;
	}
	
	public static Date parseDate(String date) {
		try {
			return SDF.parse(date);
		} catch (ParseException e) {
		}
		return null;
	}
	
	public static byte randomByte() {
		return randomBytes(1)[0];
	}
	
	public static byte[] randomBytes(int len) {
		byte[] bytes = new byte[len];
		RND.nextBytes(bytes);
		return bytes;
	}
	
	public static boolean randomBoolean() {
		return RND.nextBoolean();
	}
	
	public static int randomInt() {
		return RND.nextInt();
	}
	
	/**
	 * random a integer [min, max) 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int randomInt(int min, int max) {
		return min + randomInt(max);
	}
	
	/**
	 * random a int number [0, max)
	 * @param max
	 * @return
	 */
	public static int randomInt(int max) {
		return RND.nextInt(max);
	}
	
	public static long randomLong() {
		return RND.nextLong();
	}
	
	public static float randomFloat() {
		return RND.nextFloat();
	}
	
	public static double randomDouble() {
		return RND.nextDouble();
	}
	
	public static String randomText(int len) {
		StringBuilder buf = new StringBuilder(len);
		char[] chars = DIGITS;
		int charsLen = chars.length;
		Random rnd = RND;
		while (len-- > 0) {
			buf.append(chars[rnd.nextInt(charsLen)]);
		}
		return buf.toString();
	}
	
	public static String decodeURL(String url, String enc) {
		try {
			return URLDecoder.decode(url, enc);
		} catch (Exception e) {}
		return url;
	}
	
	public static String encodeURL(String url, String enc) {
		try {
			return URLEncoder.encode(url, enc);
		} catch (Exception e) {}
		return url;
	}
	
}
